package com.example.hrsystem.ui.attendance;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import com.example.hrsystem.repository.DailyAttendanceRepository;
import com.example.hrsystem.model.DailyAttendanceRecord;

import java.util.Date;
import java.util.List;

/**
 * 每日打卡ViewModel
 * 连接Repository和UI层，处理每日打卡相关的UI数据逻辑
 */
public class DailyAttendanceViewModel extends AndroidViewModel {

    private DailyAttendanceRepository repository;
    private LiveData<List<DailyAttendanceRecord>> allDailyAttendanceRecords;

    /**
     * 构造方法
     */
    public DailyAttendanceViewModel(@NonNull Application application) {
        super(application);
        repository = new DailyAttendanceRepository(application);
        allDailyAttendanceRecords = repository.getAllDailyAttendanceRecords();
    }

    /**
     * 获取所有每日打卡记录
     */
    public LiveData<List<DailyAttendanceRecord>> getAllDailyAttendanceRecords() {
        return allDailyAttendanceRecords;
    }

    /**
     * 获取特定员工的所有打卡记录
     */
    public LiveData<List<DailyAttendanceRecord>> getAttendanceRecordsByEmployeeId(String employeeId) {
        return repository.getAttendanceRecordsByEmployeeId(employeeId);
    }

    /**
     * 插入一条每日打卡记录
     */
    public void insert(DailyAttendanceRecord record) {
        repository.insert(record);
    }

    /**
     * 更新每日打卡记录
     */
    public void update(DailyAttendanceRecord record) {
        repository.update(record);
    }

    /**
     * 删除指定ID的每日打卡记录
     */
    public void deleteById(int id) {
        repository.deleteById(id);
    }

    /**
 * 获取指定员工在指定日期范围内的每日打卡记录
 */
    public LiveData<List<DailyAttendanceRecord>> getDailyAttendanceRecordsByDateRange(String employeeId, Date startDate, Date endDate) {
        return repository.getDailyAttendanceRecordsByDateRange(employeeId, startDate, endDate);
    }

    /**
     * 获取指定员工在指定日期范围内的每日打卡记录（同步方式）
     */
    public List<DailyAttendanceRecord> getDailyAttendanceRecordsByDateRangeSync(String employeeId, Date startDate, Date endDate) {
        return repository.getDailyAttendanceRecordsByDateRangeSync(employeeId, startDate, endDate);
    }

    /**
     * 获取指定员工在指定日期的每日打卡记录
     */
    public DailyAttendanceRecord getDailyAttendanceRecordByDate(String employeeId, Date date) {
        return repository.getDailyAttendanceRecordByDate(employeeId, date);
    }
}